package com.larissa.android.quiz.question;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.library.baseAdapters.BR;

import java.util.Date;
import java.util.UUID;

public class Question extends BaseObservable implements Parcelable {
    public static final String GEO = "GEOGRAPHY";
    public static final String MATH = "MATHEMATICS";

    private String category;
    private String questionText;
    private String questionAnswer;
    private UUID questionId;
    private String createTime;


    public Question(String category, String questionText, String questionAnswer, String createTime, UUID questionId) {
        this.category = category;
        this.questionText = questionText;
        this.questionAnswer = questionAnswer;
        this.questionId = questionId;
        this.createTime = createTime;
    }

    public Question(String questionText, String questionAnswer, UUID questionId) {
        this.questionText = questionText;
        this.questionAnswer = questionAnswer;
        this.questionId = questionId;
    }

    public Question(String questionText, String questionAnswer) {
        this.questionText = questionText;
        this.questionAnswer = questionAnswer;
    }

    public Question(String questionText, String questionAnswer, String category) {
        this.questionAnswer = questionAnswer;
        this.questionText = questionText;
        this.category = category;
        questionId = UUID.randomUUID();
        createTime = String.valueOf(new Date());
    }

    protected Question(Parcel in) {
        questionText = in.readString();
        questionAnswer = in.readString();
        questionId = (UUID) in.readSerializable();
        category = in.readString();
        createTime = in.readString();
    }

    public static final Creator<Question> CREATOR = new Creator<Question>() {
        @Override
        public Question createFromParcel(Parcel in) {
            return new Question(in);
        }

        @Override
        public Question[] newArray(int size) {
            return new Question[size];
        }
    };

    @Bindable
    public String getQuestionText() {
        return questionText;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
        notifyPropertyChanged(BR.questionText);
    }

    @Bindable
    public String getQuestionAnswer() {
        return questionAnswer;
    }

    public void setQuestionAnswer(String questionAnswer) {
        this.questionAnswer = questionAnswer;
        notifyPropertyChanged(BR.questionAnswer);
    }

    public UUID getQuestionId() {
        return questionId;
    }

    public void setQuestionId(UUID questionId) {
        this.questionId = questionId;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
        notifyPropertyChanged(BR.geo);
        notifyPropertyChanged(BR.math);
    }

    public String getCreateTime() {
        return createTime;
    }


    @Bindable
    public boolean isMath() {
        return category.equalsIgnoreCase(MATH);
    }

    public void setMath(boolean bool) {
        if (bool) {
            setCategory(MATH);
        }
    }

    @Bindable
    public boolean isGeo() {
        return category.equalsIgnoreCase(GEO);
    }

    public void setGeo(boolean bool) {
        if (bool) {
            setCategory(GEO);
        }
    }

    @Bindable
    public boolean isBoolAnswer()
    {
        return questionAnswer.equalsIgnoreCase("true");
    }

    public void setBoolAnswer(boolean bool)
    {
        if(bool)
        {
            questionAnswer = "true";
        }else
        {
            questionAnswer = "false";
        }
        notifyPropertyChanged(BR.boolAnswer);
    }




    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(questionText);
        parcel.writeString(questionAnswer);
        parcel.writeSerializable(questionId);
        parcel.writeString(category);
        parcel.writeString(createTime);
    }

    @NonNull
    public Question clone()
    {
        Question newQuestion = new Question(questionText, questionAnswer, category);
        newQuestion.createTime = createTime;
        return newQuestion;
    }

    /**
     * 判断一个Question实例的text, answer和category是否与另一个的相同
     * @param another 另一个Question实例
     * @return 是否相等
     */
    public boolean equals(Question another)
    {
        return questionText.equals(another.getQuestionText()) && questionAnswer.equals(another.questionAnswer) && category.equals(another.category);
    }

    /**
     * 判断一个Question实例的category是否与另一个的相同
     * @param another 另一个Question实例
     * @return 是否相等
     */
    public boolean hasTheSameCategoryWith(Question another)
    {
        return category.equals(another.category);
    }

}
